# -*- makefile -*-
# rules to patch the unpacked files in the source directory
# ---------------------------------------------------------------------------
# various rules to unpack addons and (un)apply patches.
# 	- patch / apply-patches
#	- unpatch / reverse-patches

.NOTPARALLEL:

patchdir	?= debian/patches
series_file	?= $(patchdir)/series

# disable all debian patches but these
debian_patches := \
                  gcc-cloog-dl \
                  libstdc++-pic \
                  deb-protoize \
		  gcc-hash-style-gnu-arm \
		  patch-draft-2-40518 \
		  copyhaste-longlong-4.4 \
		  local-variables-pic \
		  rev-162637-sfp-hfp-calling-convention \
		  pr45112 \
		  rev161833 \
		  gcc-cs2010q1 \
		  pr-46865-save-temps \
                  debuginfo-fix

ifeq (yes,$(NO_DOCS))
	debian_patches += local-no-docs
endif

series_stamp = $(stampdir)/02-series-stamp
series: $(series_stamp)
$(series_stamp):
	echo $(strip $(addsuffix .diff,$(debian_patches))) \
	  | sed -r 's/  +/ /g' | tr " " "\n" > $(series_file)
ifneq (,$(strip $(hardening_patches)))
  ifneq ($(DEB_CROSS),yes)
    ifneq ($(PKGSOURCE),gcc-snapshot)
	echo $(strip $(addsuffix .diff,$(hardening_patches))) \
	  | sed -r 's/  +/ /g' | tr " " "\n" >> $(series_file)
    endif
  endif
endif
	sed -r 's/(.)$$/\1 -p1/' -i $(series_file)
	touch $@

autotools_files := $(addprefix ./,$(foreach file,$(shell lsdiff --no-filename \
	$(foreach patch,$(debian_patches),$(patchdir)/$(patch).diff) \
	| sed -r 's/[ab]\/src\//src\//' | sort | uniq),$(shell echo $(file) \
	| egrep 'configure\.(ac|in)|Makefile\.(am|in)|acinclude.m4')))

autoconf_version = 2.59
ifeq ($(PKGSOURCE),gcc-snapshot)
  # The actual version depends on the build-dependencies set by
  # variable AUTO_BUILD_DEP in rules.conf.  Here, we assume the
  # correct version is installed.
  autoconf_version =
endif

# FIXME: the auto* stuff is done every time for every subdir, which
# leads to build errors. Idea: record the auto* calls in the patch
# files (AUTO <dir> <auto-command with options>) and run them separately,
# maybe only once per directory).
$(patch_stamp): $(unpack_stamp) $(series_stamp)
	QUILT_PATCHES=$(patchdir) \
	  quilt --quiltrc /dev/null push -a || test $$? = 2

	if ! test -f ./autotools_files ; then touch ./autotools_files ; fi

	for f in $(autotools_files) ; \
	  do case $$f in \
	    */configure.*|*/acinclude.m4) \
		if grep ^"$$(md5sum $$f)"$$ ./autotools_files >/dev/null ; \
		  then echo "Skipping already regenerated file $$f." ; \
		else \
		  echo "Running autoconf$(autoconf_version) in $$(dirname $$f)..." ; \
		  dir="$(CURDIR)"; cd $(CURDIR)/$$(dirname $$f) \
		    && AUTOM4TE=/usr/bin/autom4te$(autoconf_version) autoconf$(autoconf_version) \
		    && cd $$dir \
		    && echo "$$(md5sum $$f)" >> ./autotools_files ; \
		fi ;; \
	    */Makefile.*) ;; \
	    *) echo "Unknown file: $$f"; false; \
	  esac; \
	done

	for i in $(debian_patches); do \
	  echo -e "\n$$i:" >> pxxx; \
	  sed -n 's/^# *DP: */  /p' $(patchdir)/$$i.diff >> pxxx; \
	done
#	-$(srcdir)/move-if-change pxxx $@
	mv pxxx $@

unpatch:
	QUILT_PATCHES=$(patchdir) \
	  quilt --quiltrc /dev/null pop -a -R || test $$? = 2
	rm -rf .pc
	for f in $(autotools_files); do \
	  rm -f $$(echo $$f | sed -r 's/\.(ac|am|in)$$//'); \
	done

$(src_spu_stamp): $(patch_stamp)
	rm -rf src-spu
ifeq (,$(strip $(hardening_patches)))
	ln -s src src-spu
else
	cp -a src src-spu
	set -e; \
	for p in $(hardening_patches); do \
	  list="$$p $$list"; \
	done; \
	for p in $$list; do \
	  echo "Revert for spu build: $$p"; \
	  patch -d src-spu -p2 -R < debian/patches/$$p.diff; \
	done
endif
	set -e; \
	for p in $(spu_patches); do \
	  echo "Apply for spu build: $$p"; \
	  patch -d src-spu -p2 < debian/patches/$$p.diff; \
	done
	touch $@

patch: $(patch_stamp)
.PHONY: patch series quilt autotools
